﻿//1.全局变量

//2.初始化界面展示方法
function fn_menuTree() {
    $('#GroupMenus').tree({
    checkbox: true,
    url: "Controller.aspx?controller=GroupMenuController&method=GetGroupMenu&groupId=-1",
        
        onClick: function(node) {
            $(this).tree('toggle', node.target);
        },
        onLoadSuccess:function(node,data){
            $('#GroupMenus').tree('collapseAll');
        }
    });
}

function fn_groupGrid() {
    $('#groupGrid').datagrid({
        fit: true,
        fitColumns: true,
        rownumbers: true,
        singleSelect: true,
        url: 'Controller.aspx?controller=GroupMenuController&method=GetGroupList',
        onSelect: function(rowIndex, rowData) {
                var selected = $('#groupGrid').datagrid('getSelected');
	            if (selected) {
	                $('#GroupMenus').tree('options').url="Controller.aspx?controller=GroupMenuController&method=GetGroupMenu&groupId="+selected.GroupId;
                    $('#GroupMenus').tree('reload');
                }
            }
        });
    }
//3.初始化入口
    $(function() {

        fn_menuTree();
        fn_groupGrid();
        
        $('#dlg-binduser').dialog('close');
    });

//4.页面方法
function menugroup_save(){
    var selected = $('#groupGrid').datagrid('getSelected');
    if (selected) {
        var nodes = $('#GroupMenus').tree('getChecked');
        var s = '';
			for(var i=0; i<nodes.length; i++){
			if(nodes[i].attributes==null || nodes[i].attributes.ismodule==false){
				if (s != '') s += ',';
				s += nodes[i].id;
				}
			}
        var check={groupId:selected.GroupId,checkid:s};
        requestAjax('Controller.aspx?controller=GroupMenuController&method=SaveUserWorkGroup',check);
    }
}

function btn_binduser(){
    var selected = $('#groupGrid').datagrid('getSelected');
    if (selected) {
        $('#dlg-binduser').dialog({title:'绑定用户'});
        $('#dlg_save').linkbutton('enable');
        $('#dlg_delete').linkbutton('disable');
        $('#txtgroupId').val(selected.GroupId);
        $('#userlistGird').datagrid('options').url='Controller.aspx?controller=GroupMenuController&method=GetAllUserList';
        $('#userlistGird').datagrid('reload');
    }
}

function btn_showuser(){
    var selected = $('#groupGrid').datagrid('getSelected');
    if (selected) {
        $('#dlg-binduser').dialog({title:'查看用户'});
        $('#dlg_save').linkbutton('disable');
        $('#dlg_delete').linkbutton('enable');
        $('#txtgroupId').val(selected.GroupId);
        $('#userlistGird').datagrid('options').url='Controller.aspx?controller=GroupMenuController&method=GetGroupUserList&groupId='+selected.GroupId;
        $('#userlistGird').datagrid('reload');
    }
}

function dlg_save(){
    var selected = $('#userlistGird').datagrid('getSelections');
    if (selected.length>0) {
        var ids = [];		 
		for(var i=0;i<selected.length;i++){
			ids.push(selected[i].USER_ID);
		}
        requestAjax('Controller.aspx?controller=GroupMenuController&method=GroupAddUser', { groupId:$('#txtgroupId').val(),userids: ids.join(',') }, function(data) {
                $('#dlg-binduser').dialog('close');
            });
    }
}
function dlg_delete(){
    var selected = $('#userlistGird').datagrid('getSelections');
    if (selected.length>0) {
        var ids = [];		 
		for(var i=0;i<selected.length;i++){
			ids.push(selected[i].USER_ID);
		}
        requestAjax('Controller.aspx?controller=GroupMenuController&method=GroupDeleteUser', { groupId:$('#txtgroupId').val(),userids: ids.join(',') }, function(data) {
                $('#dlg-binduser').dialog('close');
            });
    }
}
    
    
//********************************************************************************************
//异步请求
//function requestAjax(requestUrl, requestData, callback) {
//    $.ajax({
//        type: "POST",
//        url: requestUrl,
//        data: requestData,
//        success: function(msg) {
//            var retobject = eval('(' + msg + ')');
//            if (retobject.ret == 0) {//后台操作成功
//                if (callback)
//                    callback(retobject);
//            }
//            //if(retobject.msg){
//            alert("结果: " + retobject.msg);
//            //}
//        }
//    });
//}
////Form提交数据
//function FormSubmit(formObj, urlstr,callback) {
//    formObj.form('submit', {
//        url: urlstr,
//        onSubmit: function() {

//            //alert($("#IMGUrl").val());
//            return formObj.form("validate");
//        },
//        success: function(msg) {
//            var retobject = eval('(' + msg + ')');
//            if (retobject.ret == 0) {//后台操作成功
//                //$('#dialog-product').dialog('close');
//                //$('#productGrid').datagrid('reload');
//                callback();
//            }
//            alert("结果: " + retobject.msg);
//        }
//    });
//}

                       